﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------


namespace UnityEngine.Components
{
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using UnityEngine.Core;
    using System.Xml.Serialization;
    using System.Xml;
    using UnityEngine.IO;

    public class Transform : UnityEngine.Core.Component
    {
        public Matrix WorldMatrix(Vector3 sacle, Vector3 offset)
        {
            Matrix rotation = this.RotationBefore * Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z) * this.RotationAfter;
            Matrix scaling = Matrix.CreateScale(this.Scale.X * sacle.X, this.Scale.Y * sacle.Y, this.Scale.Z * sacle.Z);
            Matrix m = rotation * scaling * Matrix.CreateTranslation(Position + offset);
            return m;
        }

        public List<Transform> Children
        {
            get;
            set;
        }

        public Matrix RotationBefore
        {
            get;
            set;
        }

        public Vector3 RotationAfterVector3
        {
            get;
            set;
        }

        public Matrix RotationAfter
        {
            get
            {
                return Matrix.CreateRotationX(RotationAfterVector3.X) * Matrix.CreateRotationY(RotationAfterVector3.Y) * Matrix.CreateRotationZ(RotationAfterVector3.Z);
            }
        }

        // position, default is Vector3.Zero
        [Editable]
        public Vector3 Position
        {
            get;
            set;
        }

        // rotation around axis X, Y, Z
        [Editable]
        public Vector3 Rotation
        {
            get;
            set;
        }

        // scale at direction of axis X, Y, Z, default is Vector3.One
        [Editable]
        public Vector3 Scale
        {
            get;
            set;
        }

        // parent
        public Transform Parent
        {
            get;
            set;
        }

        // local position
        [Editable]
        public Vector3 LocalPostion
        {
            get;
            set;
        }

        // local rotation
        [Editable]
        public Vector3 LocalRotation
        {
            get;
            set;
        }

        // local scale
        [Editable]
        public Vector3 LocalScale
        {
            get;
            set;
        }

        [Editable]
        public bool GameObjectActive
        {
            get;
            set;
        }

        public Transform()
            : this(null)
        {

        }

        public Transform(GameObject gameObject)
            : base(gameObject)
        {
            this.Children = new List<Transform>();
            this.Position = Vector3.Zero;
            this.RotationBefore = Matrix.Identity;
            this.RotationAfterVector3 = Vector3.Zero;
            this.Rotation = Vector3.Zero;
            this.Scale = Vector3.One;
            this.LocalPostion = Vector3.Zero;
            this.LocalRotation = Vector3.Zero;
            this.LocalScale = Vector3.One;
            this.Parent = null;
            this.GameObjectActive = true;
        }
    }
}
